package com.test.lyt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.test.lyt.dao.UserMapper;
import com.test.lyt.entity.dto.UserDTO;
import com.test.lyt.entity.po.UserPO;
import com.test.lyt.service.UserService;
import com.test.lyt.uils.PageResult;
import com.test.lyt.uils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;


@Service
public class userServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserPO queryDetail(UserDTO userDTO) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(userDTO.getId())) {
            queryWrapper.eq(UserPO::getId, userDTO.getId());
        }
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public PageResult<UserPO> page(UserDTO userDTO) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(UserPO::getUpdateTime);
        //对应几个筛选条件
        if (StringUtils.isNotNull(userDTO.getUserAdress())) {
            queryWrapper.like(UserPO::getUserAdress, userDTO.getUserAdress());
        }
        if (StringUtils.isNotNull(userDTO.getUserEmail())) {
            queryWrapper.like(UserPO::getUserEmail, userDTO.getUserEmail());
        }
        if (StringUtils.isNotNull(userDTO.getUserHeight())) {
            queryWrapper.like(UserPO::getUserHeight, userDTO.getUserHeight());
        }
        Page<UserPO> result = userMapper.selectPage(new Page<>(userDTO.getCurrPage(), userDTO.getPageSize()), queryWrapper);
        return new PageResult<>(result.getRecords(),result.getTotal(),result.getSize());

    }

    @Override
    public int delete(UserDTO userDTO) {
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(userDTO.getId())) {
            queryWrapper.eq(UserPO::getId, userDTO.getId());
        }
        //此处逻辑删除或者物理删除？@TableLogicDelete(value = true)
        //userMapper.deleteById(userDTO.getId());
        return userMapper.delete(queryWrapper);
    }

    @Override
    public int createOrUpdate(UserDTO userDTO) {
        if (StringUtils.isNull(userDTO.getId())) {
            UserPO userPO = UserPO.builder()
                    .uuid(UUID.randomUUID().toString())
                    .userName(userDTO.getUserName())
                    .userPhone(userDTO.getUserPhone())
                    .userEmail(userDTO.getUserEmail())
                    .userAdress(userDTO.getUserAdress())
                    .userHeight(userDTO.getUserHeight())
                    .createTime(LocalDateTime.now())
                    .updateTime(LocalDateTime.now())
                    .build();
            return userMapper.insert(userPO);
        } else {
            UserPO userPO = UserPO.builder()
                    .userName(userDTO.getUserName())
                    .userPhone(userDTO.getUserPhone())
                    .userEmail(userDTO.getUserEmail())
                    .userAdress(userDTO.getUserAdress())
                    .userHeight(userDTO.getUserHeight())
                    .updateTime(LocalDateTime.now())
                    .build();
            //此处使用LambdaUpdateWrapper显得笨笨
            //LambdaUpdateWrapper<UserPO> wrapper = new LambdaUpdateWrapper<>();
            //userMapper.update(userPO, new LambdaUpdateWrapper<UserPO>());
            return userMapper.updateById(userPO);
        }
    }
}